import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.Scanner;

/**
 * @author HuAng Software Development Group
 * @since 2022.12.7
 * @version v1.0.0.1
 */

public class Main {
    //This code was written by HuAng Software Development Group

    public static void sleep(int times){
        // sleep(time)
        try {
            Thread.sleep(times);
        } catch (Exception ignored) {
//            System.exit(1);
        }
    }

//    public static String GetClipBoard(){
//        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//        Transferable ts = clipboard.getContents(null);
//        if (ts != null) {
//            if (ts.isDataFlavorSupported(DataFlavor.stringFlavor)) {
//                try {
//                    String data = (String) ts.getTransferData(DataFlavor.stringFlavor);
//                    return data;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return null;
//    }


    public static void ToClipBoard(String str){
        // Ctrl+C
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(new StringSelection(str), null);
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("微信轰炸机  Made BY HuAng Software Development Group");
        System.out.println("正在初始化程序...请稍后");
        System.out.println("程序初始化完成");
        System.out.println("注意：本程序需要访问系统剪贴板以及辅助功能，请提前赋予相应的权限！");
        System.out.print("\n\n");
        System.out.print("请输入要轰炸的内容：");
        String message = input.nextLine();
        //message = message.replaceAll(" ", "");
        System.out.print("请输入要轰炸的次数：");
        String times_w = input.nextLine();
        times_w = times_w.replaceAll(" ", "");
        // NumberFormatException
        System.out.print("请输入要轰炸的间隔时间(ms)：");
        String r_w = input.nextLine();
        interval_w = interval_w.replaceAll(" ", "");
        // 用户的三个输入
        //异常处理，避免在次数和间隔时间中出现字符串
        try {
            int time = Integer.parseInt(times_w);
            int interval = Integer.parseInt(interval_w);
        } catch (NumberFormatException e) {
            System.out.print("\n\n");
            System.out.println("您的输入不符合规范，请重新输入");
            System.exit(1);
        }
        int time = Integer.parseInt(times_w);
        int interval = Integer.parseInt(interval_w);
        System.out.print("\n\n");
        if (time > 200){
            System.out.println("连续轰炸次数过多易导致微信崩溃，已默认为200次");
            time = 200;
        }
        if (interval < 10){
            System.out.println("连续轰炸时间过短易导致微信崩溃，已默认为10ms");
            interval = 10;
        }
        System.out.print("\n");
        System.out.println("轰炸内容：\"" + message + "\"");
        System.out.println("轰炸次数：" + time);
        System.out.println("轰炸间隔：" + interval + "ms");
        System.out.print("\n");
        System.out.print("是否确定发送(y/n):");
        String user_choose = input.nextLine();
        user_choose = user_choose.replaceAll(" ", "");
        user_choose = user_choose.toLowerCase();
        input.close();
        if (user_choose.equals("n")) {
            System.out.println("\n即将退出");
            System.exit(0);
        }
        System.out.println("\n\n");
        System.out.println("\n请将光标放置于微信输入框上");
        System.out.print("\n");
//        String clipboard = GetClipBoard();
        int i;
        for (i = 5; i > 0; i--) {
            System.out.println(i);
            sleep(1000);
        }
        ToClipBoard(message);
        System.out.println("\n\n");
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {;
        }
        if(robot == null) {
            System.out.println("抱歉，程序遇到错误，请联系开发者解决");
            System.exit(1);
        }
        robot.mousePress(KeyEvent.BUTTON1_MASK);
        robot.delay(5);
        robot.mouseRelease(KeyEvent.BUTTON1_MASK);
        for (i = 0; i != time; i++) {
            sleep(interval);
            System.out.println("正在执行第"+(i+1)+"次轰炸：\""+message+"\"");
            // Ctrl + V
//            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_V);
//            robot.delay(1);
            robot.keyRelease(KeyEvent.VK_V);
//            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_META);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        }
//        if (clipboard == null){
//            ToClipBoard("HuAng Software Development Group");
//        } else {
//            ToClipBoard(clipboard);
//        }
        ToClipBoard("");
        System.out.println("\n轰炸完毕 感谢您的使用，程序即将退出");
        System.out.println("祝您生活愉快～");
        System.exit(0);
    }
}